package com.csii.pos.query;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.util.excel.ExcelUtils;
import com.csii.adhoc.util.myabtisplus.QueryWrapperUtils;
import com.csii.adhoc.controller.BaseController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.pos.query.service.IAfSofUofService;
import com.csii.pos.query.service.IExcelTemplateService;
import com.csii.pos.query.vo.AfSofUofVO;
import com.csii.result.APIResult;
import com.csii.result.TableHtmlBuilder;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: 资金安排表
 * @Author: power-stdio
 * @Date: 2021-07-08
 * @Version: V1.0
 */

@RestController
@RequestMapping("/afSofUof")
@Slf4j
public class AfSofUofController extends BaseController<AfSofUof, IAfSofUofService> {
    @Autowired
    private IAfSofUofService afSofUofService;
    @Autowired
    private IExcelTemplateService excelTemplateService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 分页列表查询
     *
     * @param afSofUofVO 参数
     * @return 分页查询结果
     */
    @OperationLog(value = "资金安排表-分页列表查询")
    @PostMapping(value = "/queryAfSofUof")
    public APIResult<?> queryPageList(@RequestBody AfSofUofVO afSofUofVO ) {
        AfSofUof afSofUof = ConvertUtils.convert(AfSofUof.class, afSofUofVO);
        QueryWrapper<AfSofUof> queryWrapper = QueryWrapperUtils.getEqQueryWrapperFromDto(afSofUof, new QueryWrapper<>());
        if (StringUtils.isEmpty(afSofUof.getMafDt())){
            throw new ParamValidityException("时间不能为空");
        } else if (StringUtils.isEmpty(afSofUof.getMafItemType())) {
            throw new ParamValidityException("状态不能为空");
        } else if (StringUtils.isEmpty(afSofUofVO.getMafFlag())) {
            throw new ParamValidityException("标识不能为空");
        }

        queryWrapper.orderByAsc("SEQ_NO");
        queryWrapper.eq("MAF_DT", afSofUofVO.getMafDt());
        queryWrapper.eq("MAF_ITEM_TYPE", afSofUofVO.getMafItemType());
        queryWrapper.eq("MAF_FLAG", afSofUofVO.getMafFlag());

        List<AfSofUof> list = afSofUofService.list(queryWrapper);
        String html = TableHtmlBuilder.buildTableHtml(list);
        return APIResult.successResult(list).setTableHtml(html);
    }

    /**
     * @description: 输入框查询是提示接口
     * @param: hintVo
     * @return: 模糊查询list
     * @author liuzhonghua
     * @date:
     */
    @RequestMapping("queryHint")
    public APIResult<?> queryHint(@RequestBody AfSofUofVO afSofUofVO) {
        QueryWrapper<AfSofUof> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DISTINCT MAF_ITEM_NO");
        List<AfSofUof> list = afSofUofService.list(queryWrapper);
        return APIResult.successResult(list);
    }

    /**
     * 添加
     *
     * @param afSofUofVO 参数
     * @return 添加结果
     */
    @OperationLog(value = "资金安排表-添加")
    @PostMapping(value = "/add")
    public APIResult<?> add(@RequestBody AfSofUofVO afSofUofVO) {
        AfSofUof afSofUof = ConvertUtils.convert(AfSofUof.class, afSofUofVO);

        afSofUofService.save(afSofUof);
        return APIResult.successResult("添加成功！");
    }

    /**
     * 编辑
     *
     * @param afSofUofVO 参数
     * @return 编辑结果
     */
    @OperationLog(value = "资金安排表-编辑")
    @PostMapping(value = "/editAfSofUof")
    public APIResult<?> edit(@RequestBody AfSofUofVO afSofUofVO) {

        QueryWrapper<AfSofUof> queryWrapper = new QueryWrapper<>();
        AfSofUof afSofUof = ConvertUtils.convert(AfSofUof.class, afSofUofVO);
        afSofUof.setFiBcpNo("null");
        afSofUof.setFiNo("null");
        queryWrapper.eq("MAF_DT", afSofUofVO.getMafDt());
        queryWrapper.eq("MAF_ITEM_NO", afSofUofVO.getMafItemNo());
        afSofUofService.update(afSofUof,queryWrapper);
        return APIResult.successResult("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param map id
     * @return 删除结果
     */
    @OperationLog(value = "资金安排表-通过id删除")
    @PostMapping(value = "/delete")
    public APIResult<?> delete(@RequestBody Map<String, String> map) {
        afSofUofService.removeById(map.get("id"));
        return APIResult.successResult("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids id集合
     * @return 删除结果
     */
    @OperationLog(value = "资金安排表-批量删除")
    @PostMapping(value = "/deleteBatch")
    public APIResult<?> deleteBatch(@RequestBody List<AfSofUofVO> ids) {
        this.afSofUofService.removeByIds(ids.stream().map(AfSofUofVO::getId).collect(Collectors.toList()));
        return APIResult.successResult("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return 查询结果
     */
    @OperationLog(value = "资金安排表-通过id查询")
    @PostMapping(value = "/queryById")
    public APIResult<?> queryById(@RequestParam(name = "id") String id) {
        AfSofUof afSofUof = afSofUofService.getById(id);
        if (afSofUof == null) {
            return APIResult.failResult(null, "未找到对应数据");
        }
        AfSofUofVO afSofUofVO = ConvertUtils.convert(AfSofUofVO.class, afSofUof);
        return APIResult.successResult(afSofUofVO);
    }

    /**
     * 导出excel
     *
     * @param mafItemType 参数
     */
    @RequestMapping(value = "/exportXls")
    public void exportXls(HttpServletResponse response, @RequestParam String mafItemType,
                          @RequestParam String mafDt) {
        QueryWrapper<ExcelTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TABLE_NAME", "AF_SOF_UOF");
        ExcelTemplate template = excelTemplateService.getOne(queryWrapper);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        //当前日期
        String format = df.format(new Date());
        HashMap<String, String> map = new HashMap<>();
        map.put("$MAF_ITEM_TYPE", "'"+mafItemType+"'");
        map.put("$MAF_DT", mafDt);
        if ("week".equals(mafItemType)){
            map.put("$type","周计划");
        }else {
            map.put("$type","月计划");
        }
        ExcelUtils.exportTemplate(template.getTemplateName(), template.getTemplateDir(),format,map, jdbcTemplate, response);
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public APIResult<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        return APIResult.successResult(super.importExcel(file, AfSofUof.class));
    }
}

